package org.hackystat.sensor.manual.devtime;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.hackystat.sensorshell.SensorShellException;
import net.miginfocom.swing.MigLayout;

/**
 * A Swing panel for entering report entry.
 * @author Shaoxuan Zhang
 *
 */
public class ReportPanel extends JPanel {
  /** Support Serialization.*/
  private static final long serialVersionUID = -3142221451927999312L;

  /** The wrap String. */
  private static final String wrap = "wrap";
  
  ReportEntry report;
  SensorManager manager;
  
  private JFormattedTextField startTimeTextField;
  private JFormattedTextField endTimeTextField;
  private JTextField toolTextField;
  private JTextField resourceTextField;
  private JFileChooser fileChooser;
  //private JTextField labelsTextField;
  //private JLabel feedbackLabel;
  /** The time format.*/
  private final DateFormat timeFormat = 
    DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
  
  /**
   * Construct the panel.
   * @param report the associated ReportEntry.
   * @param manager the sensor manager.
   */
  public ReportPanel(ReportEntry report, SensorManager manager) {
    super(new MigLayout(HackyReporter.LAYOUT_MODE, // Layout Constraints 
                        "[right]rel[200::,grow,fill]",  // Column constraints 
                        "[][]")                       // Row constraints
    );                
    this.report = report;
    this.manager = manager;
    

    //Start Time
    add(new JLabel("Start Time"));
    startTimeTextField = new JFormattedTextField(timeFormat);
    startTimeTextField.setName("startTime");
    add(startTimeTextField, wrap);
    
    //End Time
    add(new JLabel("End Time"));
    endTimeTextField = new JFormattedTextField(timeFormat);
    endTimeTextField.setName("endTime");
    add(endTimeTextField, wrap);

    //Used Tool
    add(new JLabel("Tool"));
    toolTextField = new JTextField();
    toolTextField.setName("tool");
    add(toolTextField, wrap);
    
    //Resource
    add(new JLabel("Resource"));
    fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    resourceTextField = new JTextField();
    resourceTextField.setName("resource");
    add(resourceTextField, "width 100::, split 2");
    JButton selectFileButton = new JButton("Browse...");
    selectFileButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        int returnVal = fileChooser.showOpenDialog(ReportPanel.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
          File file = fileChooser.getSelectedFile();
          resourceTextField.setText(file.getPath());
        }
      }
    });
    add(selectFileButton, "width ::120, wrap");
    

    /*//Labels 
    add(new JLabel("Labels"));
    labelsTextField = new JTextField();
    labelsTextField.setName("labels");
    add(labelsTextField, wrap);*/
    
    //submit button
    JButton submitButton = new JButton("Submit");
    submitButton.setName("submit");
    submitButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        submit();
      }
    });
    add(submitButton, "center,span,wrap");
    /*
    feedbackLabel = new JLabel();
    feedbackLabel.setName("feedback");
    add(feedbackLabel, wrap);
    */
    updateData();
  }

  /**
   * Submit the report entry for sending.
   */
  protected void submit() {
    try {
      parseData();
      manager.processReportRecord(report);
      manager.setStatusMessage("Data sent.");
      report.setResource("");
    }
    catch (ReportValidateException e) {
      manager.setStatusMessage(e.getMessage());
    }
    catch (ParseException e) {
      manager.setStatusMessage(e.getMessage());
    }
    catch (SensorShellException e) {
      manager.setStatusMessage(e.getMessage());
    }
    updateData();
    repaint();
  }

  /**
   * Parse data from GUI components.
   * @throws ParseException error when parsing the time strings.
   * @throws ReportValidateException error in information of the report entry.
   */
  private synchronized void parseData() throws ReportValidateException, ParseException {
    report.setStartTime(timeFormat.parse(startTimeTextField.getText()));
    report.setEndTime(timeFormat.parse(endTimeTextField.getText()));
    report.setTool(toolTextField.getText());
    report.setResource(resourceTextField.getText());
    //report.setLabels(labelsTextField.getText());
    
    report.validate();
  }
  
  /**
   * Update data to GUI components.
   */
  private synchronized void updateData() {
    startTimeTextField.setText(timeFormat.format(report.getStartTime()));
    endTimeTextField.setText(timeFormat.format(report.getEndTime()));
    toolTextField.setText(report.getTool());
    //labelsTextField.setText(report.getLabels());
    repaint();
  }

  /**
   * @return the timeFormat
   */
  public DateFormat getTimeFormat() {
    return timeFormat;
  }
}
